var android3D = function(X_SCALE, center_vertice, atUp, atFront, gl, texture) {
  this.X_SCALE = X_SCALE;
  this.center_vertice = center_vertice;
  this.atUp = atUp;
  this.atFront = atFront;

  this.colorEye = new Array(0, 1, 0);                                 //绿
  this.colorHead = new Array(0xEA / 0xFF, 0x43 / 0xFF, 0x35 / 0xFF);  //红
  this.colorBody = new Array(251 / 0xFF, 188 / 0xFF, 5 / 0xFF);       //黄
  this.colorArm = new Array(52 / 0xFF, 168 / 0xFF, 83 / 0xFF);        //绿
  this.colorLeg = new Array(66 / 0xFF, 133 / 0xFF, 244 / 0xFF);       //蓝

  this.android_head = new Draw3D(gl, 'ball', texture);

  this.android_eyer = new Draw3D(gl, 'ball');
  this.android_eyel = new Draw3D(gl, 'ball');

  this.android_body = new Draw3D(gl, 'cyclinder', texture);

  this.android_armr = new Draw3D(gl, 'cyclinder', texture);
  this.android_arml = new Draw3D(gl, 'cyclinder', texture);

  this.android_legr = new Draw3D(gl, 'cyclinder', texture);
  this.android_legl = new Draw3D(gl, 'cyclinder', texture);

  this.lookat = new Array(0, 1, 20, 0, 0, 0, 0, 1, 0)
};
android3D.prototype.X_SCALE = 1;  //缩放系数，适应画布的变化
android3D.prototype.center_vertice =
    new Array(0, 0, 0, 1);                      //机器人中心点的位置
android3D.prototype.atUp = new Array(0, 1, 0);  //机器人上方向
android3D.prototype.atFront = 0;  //机器人的前方向，与屏幕向外方向的夹角
android3D.prototype.lookat;                                  //视点
android3D.prototype.perspective = new Array(30, 1, 1, 100);  //投影



android3D.prototype.key_armL_rotateA = 0;  //键盘改变的左胳膊的角度
android3D.prototype.key_armR_rotateA = 0;  //键盘改变的右胳膊的角度
android3D.prototype.keydown = function(ev) {
  switch (ev.keyCode) {
    case 65:               // A left
      this.atFront += 10;  //左转30度
      break;
    case 68:               // D right
      this.atFront -= 10;  //右转30度
      break;
    case 87:  // W up
      //前移
      this.center_vertice[0] += Math.sin(this.atFront * Math.PI / 180);
      this.center_vertice[2] += Math.cos(this.atFront * Math.PI / 180);
      break;
    case 83:  // down
      // 后移
      this.center_vertice[0] -= Math.sin(this.atFront * Math.PI / 180);
      this.center_vertice[2] -= Math.cos(this.atFront * Math.PI / 180);
      break;
    case 49:  // 1
      //右胳膊上转
      this.key_armR_rotateA += Math.PI / 30;
    case 50:  // 2
      //右胳膊下转
      if (ev.keyCode == 50) {
        this.key_armR_rotateA -= Math.PI / 30;
      }

      this.android_armr.cyclinderVertice2[1] = (this.bodyH / 2) - 0.3 -
          this.armH + this.armH * (1 - Math.cos(this.key_armR_rotateA));
      this.android_armr.cyclinderVertice2[2] =
          this.armH * Math.sin(this.key_armR_rotateA);
      break;
    case 51:  // 3
      //左胳膊上转
      this.key_armL_rotateA += Math.PI / 30;
    case 52:  // 4
      // 左胳膊下转
      if (ev.keyCode == 52) {
        this.key_armL_rotateA -= Math.PI / 30;
      }

      this.android_arml.cyclinderVertice2[1] = (this.bodyH / 2) - 0.3 -
          this.armH + this.armH * (1 - Math.cos(this.key_armL_rotateA));
      this.android_arml.cyclinderVertice2[2] =
          this.armH * Math.sin(this.key_armL_rotateA);
      break;
    default:
      console.log('无效的按键');
      break;
  }
};
/**
 * 身体各部分
 */
android3D.prototype.android_head;

android3D.prototype.android_eyer;
android3D.prototype.android_eyel;

android3D.prototype.android_body;

android3D.prototype.android_armr;
android3D.prototype.android_arml;

android3D.prototype.android_legr;
android3D.prototype.android_legl;
/**
 * bodyH 身体高度
 * armH 胳膊高
 * legH 腿高
 * bodyR 身体半径
 * alR 腿和胳膊的半径
 */
android3D.prototype.bodyH = 3;
android3D.prototype.armH = 1.9;
android3D.prototype.legH = 1.2;
android3D.prototype.bodyR = 1.5;
android3D.prototype.alR = 0.3;
android3D.prototype.EYE_R = 0.1;
/**
 * 身体各部位的颜色
 */
android3D.prototype.colorEye = new Array(0, 1, 0);  //黑
android3D.prototype.colorHead =
    new Array(0xEA / 0xFF, 0x43 / 0xFF, 0x35 / 0xFF);  //红
android3D.prototype.colorBody =
    new Array(251 / 0xFF, 188 / 0xFF, 5 / 0xFF);  //黄
android3D.prototype.colorArm =
    new Array(52 / 0xFF, 168 / 0xFF, 83 / 0xFF);  //绿
android3D.prototype.colorLeg =
    new Array(66 / 0xFF, 133 / 0xFF, 244 / 0xFF);  //蓝
android3D.prototype.setEyeColor = function(r, g, b) {
  this.colorEye[0] = r;
  this.colorEye[1] = g;
  this.colorEye[2] = b;
};
android3D.prototype.setHeadColor = function(r, g, b) {
  this.colorHead[0] = r;
  this.colorHead[1] = g;
  this.colorHead[2] = b;
};
android3D.prototype.setBodyColor = function(r, g, b) {
  this.colorBody[0] = r;
  this.colorBody[1] = g;
  this.colorBody[2] = b;
};
android3D.prototype.setArmColor = function(r, g, b) {
  this.colorArm[0] = r;
  this.colorArm[1] = g;
  this.colorArm[2] = b;
};
android3D.prototype.setLegColor = function(r, g, b) {
  this.colorLeg[0] = r;
  this.colorLeg[1] = g;
  this.colorLeg[2] = b;
};



/**
 * 画出安卓机器人
 */
android3D.prototype.drawAndroid3D = function(gl) {

  this.calMatrix();

  var body1 = new Array(0.0, this.bodyH / 2, 0.0, 1.0);
  var body2 = new Array(0.0, (-1) * this.bodyH / 2, 0.0, 1.0);
  var armL1 = new Array(1.9, (this.bodyH / 2) - 0.3, 0.0, 1.0);
  var armL2 = new Array(1.9, (this.bodyH / 2) - 0.3 - this.armH, 0, 1.0);
  var armR1 = new Array(-1.9, (this.bodyH / 2) - 0.3, 0.0, 1.0);
  var armR2 = new Array(-1.9, (this.bodyH / 2) - 0.3 - this.armH, 0, 1.0);
  var legL1 = new Array(0.6, (-1) * this.bodyH / 2, 0.0, 1.0);
  var legL2 = new Array(0.6, (-1) * this.bodyH / 2 - this.legH, 0.0, 1.0);
  var legR1 = new Array(-0.6, (-1) * this.bodyH / 2, 0.0, 1.0);
  var legR2 = new Array(-0.6, (-1) * this.bodyH / 2 - this.legH, 0.0, 1.0);
  var head = new Array(0.0, this.bodyH / 2 + 0.1, 0.0, 1.0);


  var EYE_A1 = Math.PI / 8;
  var EYE_A2 = Math.PI / 8;
  var eyeY = this.bodyR * Math.sin(EYE_A1);
  var eyeX = this.bodyR * Math.cos(EYE_A1) * Math.sin(EYE_A2);
  var eyeZ = this.bodyR * Math.cos(EYE_A1) * Math.cos(EYE_A2);

  var eyeL = new Array(head[0] + eyeX, head[1] + eyeY, head[2] + eyeZ, 1.0);
  var eyeR = new Array(head[0] - eyeX, head[1] + eyeY, head[2] + eyeZ, 1.0);



  this.android_head.drawBall(
      gl, head, [0, 1, 0], 0.5, this.bodyR, this.colorHead);

  this.android_eyer.drawBall(gl, eyeL, [0, 1, 0], 1, this.EYE_R, this.colorEye);
  this.android_eyel.drawBall(gl, eyeR, [0, 1, 0], 1, this.EYE_R, this.colorEye);

  this.android_body.drawCylinder(gl, body1, body2, this.bodyR, this.colorBody);

  this.android_armr.drawCylinder(gl, armR1, armR2, this.alR, this.colorArm);
  this.android_arml.drawCylinder(gl, armL1, armL2, this.alR, this.colorArm);

  this.android_legr.drawCylinder(gl, legR1, legR2, this.alR, this.colorLeg);
  this.android_legl.drawCylinder(gl, legL1, legL2, this.alR, this.colorLeg);
};

android3D.prototype.updateAndroid3D = function(gl) {

  this.calMatrix();

  this.android_head.updateBall(
      gl, this.android_head.ballVertice, this.android_head.ballVector);
  this.android_eyer.updateBall(
      gl, this.android_eyer.ballVertice, this.android_eyer.ballVector);
  this.android_eyel.updateBall(
      gl, this.android_eyel.ballVertice, this.android_eyel.ballVector);

  this.android_body.updateCylinder(
      gl, this.android_body.cyclinderVertice1,
      this.android_body.cyclinderVertice2);

  this.android_armr.updateCylinder(
      gl, this.android_armr.cyclinderVertice1,
      this.android_armr.cyclinderVertice2);
  this.android_arml.updateCylinder(
      gl, this.android_arml.cyclinderVertice1,
      this.android_arml.cyclinderVertice2);

  this.android_legr.updateCylinder(
      gl, this.android_legr.cyclinderVertice1,
      this.android_legr.cyclinderVertice2);
  this.android_legl.updateCylinder(
      gl, this.android_legl.cyclinderVertice1,
      this.android_legl.cyclinderVertice2);
};
/**
 * 身体各部分随身体变化的调整
 */
android3D.prototype.calMatrix = function() {
  var modelMatrix = new Matrix4();
  var a_zx;
  var a_z;
  if (this.atUp[0] == 0) {
    a_zx = Math.PI / 2;
    if (this.atUp[2] < 0) {
      a_zx = a_zx * (-1);
    }
  } else {
    if (this.atUp[2] == 0) {
      if (this.atUp[0] > 0) {
        a_zx = 0;
      } else {
        a_zx = Math.PI;
      }
    } else {
      a_zx = Math.atan(this.atUp[2] / this.atUp[0]);
      if (this.atUp[0] < 0) {
        a_zx += Math.PI;
      }
    }
  }

  var x_y_z = Math.sqrt(
      this.atUp[0] * this.atUp[0] + this.atUp[1] * this.atUp[1] +
      this.atUp[2] * this.atUp[2]);
  if (this.atUp[1] == 0) {
    a_z = Math.PI / 2;
  } else {
    a_z = Math.acos(this.atUp[1] / x_y_z);
  }

  modelMatrix.setScale(this.X_SCALE, 1, this.X_SCALE);
  modelMatrix.translate(
      this.center_vertice[0], this.center_vertice[1], this.center_vertice[2]);

  modelMatrix.rotate(180 * a_zx / Math.PI, 0, 1, 0);
  modelMatrix.rotate(180 * a_z / Math.PI, 0, 0, 1);
  modelMatrix.rotate((-1) * 180 * a_zx / Math.PI, 0, 1, 0);

  modelMatrix.rotate(this.atFront, 0, 1, 0);

  this.android_head.setModelMatrix(modelMatrix);
  this.android_eyer.setModelMatrix(modelMatrix);
  this.android_eyel.setModelMatrix(modelMatrix);

  this.android_body.setModelMatrix(modelMatrix);

  this.android_armr.setModelMatrix(modelMatrix);
  this.android_arml.setModelMatrix(modelMatrix);

  this.android_legr.setModelMatrix(modelMatrix);
  this.android_legl.setModelMatrix(modelMatrix);


  //视点和投影
  this.android_head.lookat = this.lookat;
  this.android_eyer.lookat = this.lookat;
  this.android_eyel.lookat = this.lookat;
  this.android_body.lookat = this.lookat;
  this.android_armr.lookat = this.lookat;
  this.android_arml.lookat = this.lookat;
  this.android_legr.lookat = this.lookat;
  this.android_legl.lookat = this.lookat;

  this.android_head.perspective = this.perspective;
  this.android_eyer.perspective = this.perspective;
  this.android_eyel.perspective = this.perspective;
  this.android_body.perspective = this.perspective;
  this.android_armr.perspective = this.perspective;
  this.android_arml.perspective = this.perspective;
  this.android_legr.perspective = this.perspective;
  this.android_legl.perspective = this.perspective;
};